package action;

import persistency.CustomerRepository;
import persistency.TaskRepository;
import model.Customer;

import com.opensymphony.xwork2.ActionSupport;

public class AddCustomer extends ActionSupport  {

private static final long serialVersionUID = 1L;
	
	private Customer customerBean;

	@Override
	public String execute() throws Exception {
		
		//call Service class to store customerBean's state in database
		CustomerRepository cr = new CustomerRepository();
		cr.insert(this.customerBean);
		return SUCCESS;
		
	}
	
	public Customer getCustomerBean() {
		return customerBean;
	}

	public void setCustomerBean(Customer customerBean) {
		this.customerBean = customerBean;
	}
	
	public void validate(){
		
		if ( customerBean.getCategoryCode().length() == 0 ){	

			addFieldError( "customerBean.categoryCode", "Category is required." );
			
		}
		
				
		if ( customerBean.getTypeCode() == 0 ){	

			addFieldError( "customerBean.typecode", "Typecode is required." );
			
		}
		
		if ( customerBean.getName().length() == 0 ){	

			addFieldError( "customerBean.name", "Name is required." );
			
		}
		if ( customerBean.getStreet().length() == 0 ){	

			addFieldError( "customerBean.street", "Street is required." );
			
		}
		if ( customerBean.getNumber().length() == 0 ){	

			addFieldError( "customerBean.number", "Number is required." );
			
		}
		if ( customerBean.getMailbox().length() == 0 ){	

			addFieldError( "customerBean.mailbox", "Mailbox is required." );
			
		}
		if ( customerBean.getPostalCode().length() == 0 ){	

			addFieldError( "customerBean.postalcode", "Postalcode is required." );
			
		}
		if ( customerBean.getCity().length() == 0 ){	

			addFieldError( "customerBean.city", "City is required." );
			
		}
		if ( customerBean.getTelephone().length() == 0 ){	

			addFieldError( "customerBean.telephone", "Telephone is required." );
			
		}
		if ( customerBean.getEmail().length() == 0 ){	

			addFieldError( "customerBean.email", "Email is required." );
			
		}
		if ( customerBean.getIban().length() == 0 ){	

			addFieldError( "customerBean.iban", "IBAN is required." );
			
		}
		if ( customerBean.getBic().length() == 0 ){	

			addFieldError( "customerBean.bic", "BIC is required." );
			
		}
		
		
	}


}
